<template>
  <div>
    <h2>文章列表：</h2>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="_id" label="文章ID" align="center">
      </el-table-column>
      <el-table-column prop="title" label="文章名称" align="center">
      </el-table-column>
      <el-table-column
        prop="synopsis"
        label="文章简介"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column label="文章分类" align="center">
        <template slot-scope="{ row }">
          <el-tag :color="$randomColor()">{{
            row.classId.classname || ""
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否展示" align="center">
        <template slot-scope="{ row }">
          <el-tag :color="$randomColor()">{{
            row.status == "1" ? "展示" : "不展示"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_time" label="创建时间" align="center">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/articleEdit/${row._id}`)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="lookArticle(row._id, row.status)"
            >{{ row.status == "1" ? "下架文章" : "展示文章" }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteArticle(row.title, row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getArticlelist,
  getArticleLook,
  getArticledelete,
} from "@/api/index.js";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  mounted() {
    this.getArticlelist();
  },
  methods: {
    getArticlelist() {
      getArticlelist().then((res) => {
        this.tableData = res.data.items;
      });
    },
    // 展示用户
    lookArticle(id, status) {
      if (status == "1") {
        getArticleLook(id, { status: "2" }).then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.msg);
            this.getArticlelist();
          }
        });
      } else {
        getArticleLook(id, { status: "1" }).then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.msg);
            this.getArticlelist();
          }
        });
      }
    },
    // 删除文章
    deleteArticle(title, id) {
      this.$confirm(`此操作将永久删除 ${title}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getArticledelete(id).then((res) => {
            if (res.data.status == 200) {
              this.$message.success(res.data.msg);
              this.getArticlelist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
